package com.example.studentsmanagersystem;

import android.util.Log;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.util.Map;

import static android.content.ContentValues.TAG;

public class ConnectionUtils {

    public static String postRequest(String url, Map<String, String> rawParams) {
        String result = null;
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder formBuilder = new FormBody.Builder();
        for (String key : rawParams.keySet()) {
            formBuilder.add(key, rawParams.get(key));
        }
        RequestBody formBody = formBuilder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                result = response.body().string();
                Log.i(TAG, "result-------->" + result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
